﻿using System;
using System.Collections.Generic;
using System.Linq;
using EntityFrameworkExtension;
using EntityFrameworkExtension.Interface;
using MainProcessor.Caching;
using MainProcessor.Interface;
using NUnit.Framework;
using Rhino.Mocks;

namespace Test.Main.Caching
{
    [TestFixture]
    public class ControllerTests 
    {

        private IDataCache cache;
        private IDataController controller;
        private IEfContext efContext;
        private IRepositoryFlush repositoryFlush;

        private const int inputId = 42;
        private static readonly Guid FundGuid = Guid.NewGuid();

        private static readonly List<MutableReferenceTable1> FundIdMappings =
            new List<MutableReferenceTable1> { new MutableReferenceTable1() { Id = inputId, Guid = FundGuid } };
        [SetUp]
        public void SetUp()
        {
            cache = MockRepository.GenerateMock<IDataCache>();
            efContext = MockRepository.GenerateMock<IEfContext>();
            repositoryFlush = MockRepository.GenerateMock<IRepositoryFlush>();
            controller = new DataController(cache, repositoryFlush);
        }

        [TearDown]
        public void Teardown()
        {
            efContext.VerifyAllExpectations();
            cache.VerifyAllExpectations();
            repositoryFlush.VerifyAllExpectations();
        }

        [Test]
        public void WillOnlyCallFindWhenDataIsinMapping()
        {
            cache.Expect(x => x.GetTypeMapping<MutableReferenceTable1>(efContext)).Return(FundIdMappings);

            var results = controller.GetGuidIntMappings1(inputId, efContext);
            Assert.AreEqual(FundGuid, results);
        }

        [Test]
        public void WillCallFindAndRefreshWhenDataIsNotinMappingButInDb()
        {
            cache.Expect(x => x.GetTypeMapping<MutableReferenceTable1>(efContext)).Return(Enumerable.Empty<MutableReferenceTable1>().ToList()).Repeat.Once();

            cache.Expect(x => x.GetTypeMappingWithRefresh<MutableReferenceTable1>(efContext)).Return(FundIdMappings).Repeat.Once();

            var results = controller.GetGuidIntMappings1(inputId, efContext);

            Assert.AreEqual(FundGuid, results);
        }

        [Test]
        public void WillCallCreateWhenNotFound()
        {
            cache.Expect(x => x.GetTypeMapping<MutableReferenceTable1>(efContext)).Return(Enumerable.Empty<MutableReferenceTable1>().ToList()).Repeat.Once();

            cache.Expect(x => x.GetTypeMappingWithRefresh<MutableReferenceTable1>(efContext)).Return(Enumerable.Empty<MutableReferenceTable1>().ToList()).Repeat.Once();

            repositoryFlush.Expect(x => x.CreateGuidIdMapping<MutableReferenceTable1> (inputId)).Return(FundGuid).Repeat.Once();

            var results = controller.GetGuidIntMappings1(inputId, efContext);

            Assert.AreEqual(FundGuid, results);
        }

       


    }
}
